package classes.graphicalEngine
{
	import flash.events.*;
	import flash.geom.*;
	import flash.media.*;
	import flash.net.*;
	import flash.utils.*;
	
	import mx.collections.ArrayCollection;
	import mx.core.*;
	
	public class Level
	{
		protected static var instance:Level = null;
		
		protected static const TimeBetweenLevelElements:Number = 2;
		protected var timeToNextLevelElement:Number = 0;
		protected var levelElementGraphics:ArrayCollection = new ArrayCollection();
		protected var fullMapRemoteData:ArrayCollection = new ArrayCollection();// <--------------------
		protected var fullMap:Array;
		protected static var visibleMap:ArrayCollection;
		
		// default map dimensions
		protected var defaultMapWidth:Number = GameObjectManager.defaultMapWidth;
		protected var defaultMapHeight:Number = GameObjectManager.defaultMapHeight;

		static public function get Instance():Level
		{
			if ( instance == null )
				instance = new Level();
			return instance;
		}
		
		public function Level(caller:Function = null )
		{
			if ( Level.instance != null )
			throw new Error( "Only one Singleton instance should be instantiated" );
			
			levelElementGraphics.addItem(ResourceManager.Cacao1Graphics);
			levelElementGraphics.addItem(ResourceManager.Cacao2Graphics);
			levelElementGraphics.addItem(ResourceManager.ChampGraphics);
			levelElementGraphics.addItem(ResourceManager.HomeGraphics);
			levelElementGraphics.addItem(ResourceManager.Latex1Graphics);
			levelElementGraphics.addItem(ResourceManager.Latex2Graphics);
			levelElementGraphics.addItem(ResourceManager.ManiocaGraphics);
			levelElementGraphics.addItem(ResourceManager.SojaGraphics);
			
			visibleMap = GameObjectManager.visibleMap;

			// pour tous les objets de la map :
			for (var i:int = 0; i < visibleMap.length; i++)
			{
				// une image - GraphicsResource
				//var graphics:GraphicsResource = levelElementGraphics.getItemAt(MathUtils.randomInteger(0, levelElementGraphics.length)) as GraphicsResource;
				var graphics:GraphicsResource = ResourceManager.selectGraphicsResource(visibleMap[i].name);
	
				// un gameObject sorti de la pool si dispo ou bien créé sinon...
				// les objets dans la pool sont d'anciens objets créés non utilisés qu'on n'a pas supprimé
				// pour éviter le passage du garbage collector et/ou des new inutiles...
				var backgroundLevelElement:BackgroundLevelElement =  BackgroundLevelElement.pool.ItemFromPool as BackgroundLevelElement;
				backgroundLevelElement.startupBackgroundLevelElement(
					graphics, 
					new Point(visibleMap[i].X - GameCamera.position.x, visibleMap[i].Y - GameCamera.position.y),
					ZOrders.BackgoundZOrder);
				var newGPSAddress:Point = new Point();
				for (var j:int = 0; j < graphics.bitmap.height; j += GameObjectManager.defaultGrapicsSizeMultiple)
				{
					newGPSAddress.y = int((visibleMap[i].Y + j)/GameObjectManager.defaultGrapicsSizeMultiple);
					for (var k:int = 0; k < graphics.bitmap.width; k += GameObjectManager.defaultGrapicsSizeMultiple)
					{
						newGPSAddress.x = int((visibleMap[i].X + k)/GameObjectManager.defaultGrapicsSizeMultiple);
						GameObjectManager.carteGPS[newGPSAddress.y][newGPSAddress.x] = 1;
					}
				}
			}
		}
		
		public function startup():void
		{
			timeToNextLevelElement = 0;
			new Player().startupPlayer(320,160);
		}
		
		public function shutdown():void
		{
			
		}
		
		public function enterFrame(dt:Number):void
		{
/*			timeToNextLevelElement -= dt;

			if (timeToNextLevelElement <= 0)
			{
				timeToNextLevelElement = TimeBetweenLevelElements;
				var graphics:GraphicsResource = levelElementGraphics.getItemAt(MathUtils.randomInteger(0, levelElementGraphics.length)) as GraphicsResource;
				var backgroundLevelElement:BackgroundLevelElement =  BackgroundLevelElement.pool.ItemFromPool as BackgroundLevelElement;
				backgroundLevelElement.startupBackgroundLevelElement(
					graphics, 
					new Point(Math.random() * defaultMapWidth, -graphics.bitmap.height),
					ZOrders.BackgoundZOrder,
					50);
			}
*/
			
		}
	}
}